package com.dapangdong.testdemo.controller.test;


import cn.afterturn.easypoi.entity.ImageEntity;
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.dapangdong.testdemo.Entiy.User;
import com.dapangdong.testdemo.utils.ExcelUtil;
import com.dapangdong.testdemo.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Excel导入导出
 * </p>
 *
 * @author : yhd
 * @program: gulimall
 * @since : 2023-05-25 15:59
 */
@RequestMapping("/excel")
@Slf4j
@RestController
@Api(tags = "Excel导入导出")
public class ExcelController {


    /**
     * excel批量导出
     */
    @GetMapping("/export")
    @ApiOperation(value = "导出", notes = "excel批量导出")
    public void exportUsersToExcel(HttpServletResponse response) {
        try {
            //从数据库查询到数据
            List<User> list = getDateList();
            //设置信息头，告诉浏览器内容为excel类型
            response.setHeader("content-Type", "application/vnd.ms-excel");
            //设置下载名称
            response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("用户信息表.xlsx", StandardCharsets.UTF_8.name()));
            //字节流输出
            ServletOutputStream out = response.getOutputStream();
            //设置excel参数
            ExportParams params = new ExportParams();
            //设置sheet名名称
            params.setSheetName("学生列表");

            Workbook workbook = ExcelExportUtil.exportExcel(params, User.class, list);
            workbook.write(out);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @ApiOperation(value = "根据注解导出Excel")
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, HttpServletRequest request) {
        //图片使用String image
        List<User> list = getDateList();
        ExcelUtil.exportExcel(list, "注解导出Excel", "自定义sheet", User.class, response, request);
    }


    @ApiOperation(value = "根据模板导出excel")
    @GetMapping("/exportExcelByTemplate")
    public void exportExcelByTemplate(HttpServletResponse response) throws IOException {
        Map<String, Object> map = new HashMap<>(3);
        List<User> list = getDateList();
        map.put("data", list);
        ExcelUtil.exportExcelByTemplate(map, "template/excelTemplate.xlsx", "根据模板导出excel", true, response, "sheet100", "sheet200");
    }


    public List<User> getDateList() {
        List<User> list = new ArrayList<>();
        for (long i = 1; i <= 3; i++) {
            ImageEntity image = new ImageEntity();
//            image.setUrl("./template/image/avatar.png");
            image.setUrl("http://101.33.204.11:9000/test/0233b8096f53496ca3dc583c692e7c07.jpg");


            User user = User.builder()
                    .id(i)
                    .name("我是：" + i)
                    .age((int) i)
//                    .image("template/image/avatar.png")
                    .image("http://101.33.204.11:9000/test/0233b8096f53496ca3dc583c692e7c07.jpg")
                    .imageEntity(image)
                    .much(BigDecimal.valueOf(100).divide(BigDecimal.valueOf(i), 4, RoundingMode.HALF_UP))
                    .localDateTime(LocalDateTime.now())
                    .build();
            list.add(user);
        }
        return list;
    }


    public static void main(String[] args) {
        String str =
                "命令单号：WO20230530003\n" +
                        "检验车间：南山线04\n" +
                        "检验日期：2023-05-30\n" +
                        "检验详情：隐蔽留痕率 0%\n" +
                        "以上命令单质检留痕率过低，请督促处理！";
        System.out.println(StringUtil.getBytes(str));
    }
}
